﻿using DevExpress.XtraEditors;
using Lamex.Common;
using Lamex.Library;
using Lamex.mNC.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mNC.UI
{
    public partial class FrmNC_CompareFIle : frmBaseChild
    {
        List<NC_FileContent> lstFile = new List<NC_FileContent>();
        public FrmNC_CompareFIle()
        {
            InitializeComponent();
        }

        private void btn_OpenSelect_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            FolderBrowserDialog fb = new FolderBrowserDialog();
            //设置默认根目录是桌面
            folder.RootFolder = Environment.SpecialFolder.Desktop;
            folder.SelectedPath = @"D:\Temp";//用绝对路径时不能正确指向
            
            //设置对话框说明
            folder.Description = "请选择文件搜索路径:";
            if (folder.ShowDialog() == DialogResult.OK)
            {
                txt_FilePath.Text = folder.SelectedPath;
               
                
            }
        }

        public static DataTable TxtToDt(string filePath, int n, DataTable dt) //这个dt 是个空白的没有任何行列的DataTable
        {
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(filePath, System.Text.Encoding.Default, false);
                String csvSplitBy = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)";
                dt.Columns.Add("Path"); //增加列标题
                int i = 0, m = 0;
                reader.Peek();
                while (reader.Peek() > 0)
                {

                    m = m + 1;
                    string str = reader.ReadLine();
                    if (m >= n + 1)
                    {
                        MatchCollection mcs = Regex.Matches(str, "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)");
                        i = 0;
                        System.Data.DataRow dr = dt.NewRow();
                        foreach (Match mc in mcs)
                        {
                            string name = mc.Value.Replace("\"", "");
                            dr[i] = name;
                            i++;
                        }
                        dt.Rows.Add(dr);  //DataTable 增加一行 

                    }
                }
                reader.Dispose();

                reader.Close();
                return dt;
            }
            catch (Exception ex)
            {


                MessageBox.Show(ex.Message);
                reader.Close();
                return null;
            }
        }

        private void btnSelectCompare_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> lst = new List<string>();
                
                DataTable dtfiel01 = new DataTable();
                DataTable dtfiel02 = new DataTable();
                OpenFileDialog dialog = new OpenFileDialog();

                dialog.Multiselect = true;//该值确定是否可以选择多个文件

                dialog.Title = txt_FilePath.Text;
                

                dialog.Filter = "所有文件(*.*)|*.*";

                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    
                    string[] NcFiles = dialog.FileNames;
                    //var NcFiles = new DirectoryInfo(dialog.).GetFiles();
                    if (NcFiles.Count() != 2)
                    {
                        MessageBox.Show("比对文件数量小于1或大于2");
                        return;
                    }
                    //文件比对
                    foreach (string info in NcFiles)
                    {
                        //string FileName = info.Substring(info.LastIndexOf("\\") + 1);
                        if (File.Exists(info))
                        {
                            string targetPatp = info;
                            if (File.Exists(targetPatp))
                            {
                                lst.Add(targetPatp);
                            }
                        }
                    }
                    string[] strArray = lst.ToArray();
                    TxtToDt(strArray[0].ToString(), 0, dtfiel01);
                    TxtToDt(strArray[1].ToString(), 0, dtfiel02);
                    if (dtfiel01.Rows.Count != dtfiel02.Rows.Count)
                    {
                        MessageBox.Show("行数不一致！");
                        return;
                    }
                    int iRow = 0;
                    for (int i = 0; i < dtfiel01.Rows.Count; i++)
                    {
                        NC_FileContent model = new NC_FileContent();
                        if (dtfiel01.Rows[i]["Path"].ToString().Equals(dtfiel02.Rows[i]["Path"].ToString()) && dtfiel01.Rows[i]["Path"].ToString().Length == dtfiel02.Rows[i]["Path"].ToString().Length)
                        {
                            model.No = iRow;
                            model.FileContent01 = dtfiel01.Rows[i]["Path"].ToString();
                            model.FileContent02 = dtfiel02.Rows[i]["Path"].ToString();
                            model.IsFlag = true;
                        }
                        else
                        {
                            model.No = iRow;
                            model.FileContent01 = dtfiel01.Rows[i]["Path"].ToString();
                            model.FileContent02 = dtfiel02.Rows[i]["Path"].ToString();
                            model.IsFlag = false;
                            string[] strFileArray01 = dtfiel01.Rows[i]["Path"].ToString().Split(' ');
                            string[] strFileArray02 = dtfiel02.Rows[i]["Path"].ToString().Split(' ');
                            string strError = "";
                            if (dtfiel01.Rows[i]["Path"].ToString().Length == dtfiel02.Rows[i]["Path"].ToString().Length)
                            {
                                for (int m = 0; m < strFileArray01.Length; m++)
                                {
                                    if (strFileArray01[m].ToString() != strFileArray02[m].ToString())
                                    {
                                        strError += strFileArray01[m].ToString() + ",";
                                    }
                                }
                                model.IsErrorString = strError;
                            }
                            else
                            {
                                MessageBox.Show("数据长度不一致");
                                return;
                            }
                        }
                        lstFile.Add(model);
                        iRow++;
                    }

                    gridView1.OptionsView.ColumnAutoWidth = true;
                    gridView1.BestFitColumns();
                    gv01.DataSource = lstFile;
                }
            
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void chkSelet_TextChanged(object sender, EventArgs e)
        {
            List<NC_FileContent> lst = new List<NC_FileContent>();
            if (chkSelet.Checked==true)
                lst = lstFile.Where(p => p.IsFlag == true).ToList();
            else
                lst = lstFile.Where(p => p.IsFlag == false).ToList();

            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.BestFitColumns();
            gv01.DataSource = lst;
        }

        private void chkSelet_EditValueChanged(object sender, EventArgs e)
        {
            List<NC_FileContent> lst = new List<NC_FileContent>();
            if (chkSelet.Checked == true)
                lst = lstFile.Where(p => p.IsFlag == true).ToList();
            else
                lst = lstFile.Where(p => p.IsFlag == false).ToList();

            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.BestFitColumns();
            gv01.DataSource = lst;
        }
    }
}
